
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>05-MongoDB · 千锋大前端小册-GP19-Node.js</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="古艺散人">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="06-socket.html" />
    
    
    <link rel="prev" href="04-Koa2.html" />
    

    
        <link rel="shortcut icon" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    01-Node.js基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02-yarn.html">
            
                <a href="02-yarn.html">
            
                    
                    02-Yarn入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03-Express.html">
            
                <a href="03-Express.html">
            
                    
                    03-Express
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04-Koa2.html">
            
                <a href="04-Koa2.html">
            
                    
                    04-Koa2
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.5" data-path="05-MongoDB.html">
            
                <a href="05-MongoDB.html">
            
                    
                    05-MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06-socket.html">
            
                <a href="06-socket.html">
            
                    
                    06-socket编程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    高级
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    01-Node中间件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../advanced/02-Node事件循环.html">
            
                <a href="../advanced/02-Node事件循环.html">
            
                    
                    02-Node事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../advanced/03-Node中间层.html">
            
                <a href="../advanced/03-Node中间层.html">
            
                    
                    03-Node中间层
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../thanks/">
            
                <a href="../thanks/">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >05-MongoDB</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x2170;&#x3001;mongodb"><b></b>&#x2160;&#x3001;MongoDB</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;"><b></b>&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;"><b></b>&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;"><b></b>&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;"><b></b>&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;"><b></b>&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;"><b></b>&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x2171;&#x3001;mongoose"><b></b>&#x2161;&#x3001;Mongoose</a></li><ul><ul><li><span class="title-icon "></span><a href="#1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"><b></b>1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</a></li><li><span class="title-icon "></span><a href="#2&#x3001;route"><b></b>2&#x3001;Route</a></li><li><span class="title-icon "></span><a href="#3&#x3001;model"><b></b>3&#x3001;Model</a></li><li><span class="title-icon "></span><a href="#4&#x3001;view"><b></b>4&#x3001;View</a></li><li><span class="title-icon "></span><a href="#5&#x3001;controller"><b></b>5&#x3001;Controller</a></li></ul></ul></ul></div><a href="#&#x2170;&#x3001;mongodb" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x2170;&#x3001;mongodb"><a name="&#x2170;&#x3001;mongodb" class="anchor-navigation-ex-anchor" href="#&#x2170;&#x3001;mongodb"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2170;&#x3001;mongodb" class="plugin-anchor" href="#&#x2170;&#x3001;mongodb"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2160;&#x3001;MongoDB</h1>
<h3 id="&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;"><a name="&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;" class="plugin-anchor" href="#&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;&#x5B89;&#x88C5;&#x6570;&#x636E;&#x5E93;</h3>
<p><a href="https://docs.mongodb.com/manual/administration/install-community/" target="_blank">https://docs.mongodb.com/manual/administration/install-community/</a></p>
<h3 id="&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;"><a name="&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;" class="plugin-anchor" href="#&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;&#x542F;&#x52A8;&#x6570;&#x636E;&#x5E93;</h3>
<h5 id="1&#x3001;windows"><a name="1&#x3001;windows" class="anchor-navigation-ex-anchor" href="#1&#x3001;windows"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;windows" class="plugin-anchor" href="#1&#x3001;windows"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;windows</h5>
<pre><code class="lang-s">mongod --dbpath d:/data/db
mongo
</code></pre>
<h5 id="2&#x3001;mac"><a name="2&#x3001;mac" class="anchor-navigation-ex-anchor" href="#2&#x3001;mac"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;mac" class="plugin-anchor" href="#2&#x3001;mac"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;mac</h5>
<pre><code class="lang-s">mongod --config /usr/local/etc/mongod.conf
mongo
</code></pre>
<h3 id="&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;"><a name="&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;" class="plugin-anchor" href="#&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;</h3>
<pre><code class="lang-s">use gp145
db/db.getName()
show dbs
db.createCollection(&apos;movies&apos;)
db.stats()
db.version()
db.getMongo() //connection to 127.0.0.1:27017
db.dropDatabase()
</code></pre>
<h3 id="&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;"><a name="&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;" class="plugin-anchor" href="#&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;&#x96C6;&#x5408;&#x64CD;&#x4F5C;</h3>
<pre><code>db.createCollection(&apos;users&apos;)
db.getCollectionNames()
</code></pre><h3 id="&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;"><a name="&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;" class="plugin-anchor" href="#&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94;&#x3001;&#x6587;&#x6863;&#x7684;&#x64CD;&#x4F5C;</h3>
<h4 id="51-&#x6DFB;&#x52A0;"><a name="51-&#x6DFB;&#x52A0;" class="anchor-navigation-ex-anchor" href="#51-&#x6DFB;&#x52A0;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="51-&#x6DFB;&#x52A0;" class="plugin-anchor" href="#51-&#x6DFB;&#x52A0;"><i class="fa fa-link" aria-hidden="true"></i></a>5.1 &#x6DFB;&#x52A0;</h4>
<pre><code class="lang-js">db.users.insertOne({username: <span class="hljs-string">&apos;yangli&apos;</span>, password: <span class="hljs-string">&apos;abc123&apos;</span>})
db.users.insertOne({username: <span class="hljs-string">&apos;haozeliang&apos;</span>, email: <span class="hljs-string">&apos;hzl@126.com&apos;</span>})
db.users.insertOne({<span class="hljs-string">&quot;username&quot;</span>: <span class="hljs-number">1</span>, password: <span class="hljs-number">123</span>})
db.users.insertMany([{username: <span class="hljs-string">&apos;gaojie&apos;</span>, password: <span class="hljs-string">&apos;gj&apos;</span>, email: <span class="hljs-string">&apos;gj@126.com&apos;</span>}, {username: <span class="hljs-string">&apos;xinyi&apos;</span>, password: <span class="hljs-number">123</span>, email: <span class="hljs-number">123</span>}])
db.users.insert([{username: <span class="hljs-string">&apos;yangli&apos;</span>}, {useranme: <span class="hljs-string">&apos;zeliang&apos;</span>}])
db.users.save()
</code></pre>
<h4 id="52-&#x4FEE;&#x6539;"><a name="52-&#x4FEE;&#x6539;" class="anchor-navigation-ex-anchor" href="#52-&#x4FEE;&#x6539;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="52-&#x4FEE;&#x6539;" class="plugin-anchor" href="#52-&#x4FEE;&#x6539;"><i class="fa fa-link" aria-hidden="true"></i></a>5.2 &#x4FEE;&#x6539;</h4>
<pre><code class="lang-js">db.users.update({username: <span class="hljs-string">&apos;yangli&apos;</span>}, {username: <span class="hljs-string">&apos;yl&apos;</span>})

<span class="hljs-comment">// 1&#x3001;&#x5982;&#x679C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x540E;&#x8FB9;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x65E0;&#x6548;</span>
<span class="hljs-comment">// 2&#x3001;&#x5982;&#x679C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x901A;&#x8FC7;$set&#x8BBE;&#x7F6E;&#x7684;&#x8BDD;&#xFF0C; &#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x624D;&#x6709;&#x6548;</span>
<span class="hljs-comment">// 3&#x3001;&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;true/&#x5982;&#x679C;&#x6570;&#x636E;&#x67E5;&#x8BE2;&#x4E0D;&#x5230;&#xFF0C;&#x5C31;&#x521B;&#x5EFA; false/&#x5982;&#x679C;&#x6570;&#x636E;&#x67E5;&#x8BE2;&#x4E0D;&#x5230;&#xFF0C;&#x5C31;&#x4EC0;&#x4E48;&#x90FD;&#x4E0D;&#x505A;</span>
<span class="hljs-comment">// 4&#x3001;&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7B2C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;true/&#x66F4;&#x65B0;&#x591A;&#x6761;&#xFF0C;false/&#x66F4;&#x65B0;&#x4E00;&#x6761;</span>
db.users.update({username: <span class="hljs-string">&apos;gp145&apos;</span>}, {$set: {username: <span class="hljs-string">&apos;yl&apos;</span>}}, <span class="hljs-literal">true</span>, <span class="hljs-literal">true</span>)
<span class="hljs-comment">// 5&#x3001;&#x5982;&#x679C;&#x4F7F;&#x7528;updateMany, &#x5C31;&#x4E0D;&#x9700;&#x8981;&#x4F20;&#x9012;&#x540E;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7B2C;&#x4E8C;&#x4E2A;&#x4E86;</span>
db.users.updateMany({username: <span class="hljs-string">&apos;yl&apos;</span>}, {$set: {username: <span class="hljs-string">&apos;yangli&apos;</span>}})
</code></pre>
<pre><code class="lang-js"> db.collection.update(
   &lt;query&gt;,
   &lt;update&gt;,
   {
     upsert: &lt;boolean&gt;,
     multi: &lt;boolean&gt;,
     writeConcern: &lt;document&gt;,
     collation: &lt;document&gt;,
     arrayFilters: [ &lt;filterdocument1&gt;, ... ],
     hint:  &lt;document|string&gt;  // Available starting in MongoDB 4.2
   }
)
</code></pre>
<blockquote>
<p>&#x53C2;&#x8003;&#x6587;&#x6863;&#xFF1A;<a href="https://docs.mongodb.com/manual/reference/method/db.collection.update/#db.collection.update" target="_blank">https://docs.mongodb.com/manual/reference/method/db.collection.update/#db.collection.update</a></p>
</blockquote>
<h4 id="53-&#x5220;&#x9664;"><a name="53-&#x5220;&#x9664;" class="anchor-navigation-ex-anchor" href="#53-&#x5220;&#x9664;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="53-&#x5220;&#x9664;" class="plugin-anchor" href="#53-&#x5220;&#x9664;"><i class="fa fa-link" aria-hidden="true"></i></a>5.3 &#x5220;&#x9664;</h4>
<pre><code class="lang-js">db.users.remove({username: <span class="hljs-string">&apos;xinyi&apos;</span>}, <span class="hljs-literal">true</span>)
</code></pre>
<blockquote>
<p>&#x53C2;&#x8003;&#x6587;&#x6863;&#xFF1A;<a href="https://docs.mongodb.com/manual/reference/method/db.collection.remove/" target="_blank">https://docs.mongodb.com/manual/reference/method/db.collection.remove/</a></p>
</blockquote>
<h4 id="54-&#x67E5;&#x627E;"><a name="54-&#x67E5;&#x627E;" class="anchor-navigation-ex-anchor" href="#54-&#x67E5;&#x627E;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="54-&#x67E5;&#x627E;" class="plugin-anchor" href="#54-&#x67E5;&#x627E;"><i class="fa fa-link" aria-hidden="true"></i></a>5.4 &#x67E5;&#x627E;</h4>
<pre><code class="lang-js">db.movies.find({}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>})
db.movies.find({}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).sort({rt: <span class="hljs-number">-1</span>})
db.movies.find({}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).limit(<span class="hljs-number">10</span>)
db.movies.find({}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).sort({rt: <span class="hljs-number">-1</span>}).limit(<span class="hljs-number">10</span>)
db.movies.find({}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).sort({rt: <span class="hljs-number">-1</span>}).limit(<span class="hljs-number">3</span>).skip(<span class="hljs-number">6</span>)
db.movies.find({rt: {$gte: <span class="hljs-string">&apos;2019-10-14&apos;</span>}}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>})
db.movies.find({rt: {$gte: <span class="hljs-string">&apos;2019-10-14&apos;</span>}}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>})
db.movies.find({rt: {$gte: <span class="hljs-string">&apos;2019-10-14&apos;</span>}}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).count()
db.movies.find({rt: {$lte: <span class="hljs-string">&apos;2019-10-14&apos;</span>}}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).count()
db.movies.find({nm: <span class="hljs-regexp">/&#x5C0F;/</span>}, {nm: <span class="hljs-number">1</span>, _id: <span class="hljs-number">0</span>, rt: <span class="hljs-number">1</span>}).sort({rt: <span class="hljs-number">-1</span>})
</code></pre>
<h3 id="&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;"><a name="&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;" class="anchor-navigation-ex-anchor" href="#&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;" class="plugin-anchor" href="#&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516D;&#x3001;&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x5DE5;&#x5177;</h3>
<p>robo 3T</p>
<h1 id="&#x2171;&#x3001;mongoose"><a name="&#x2171;&#x3001;mongoose" class="anchor-navigation-ex-anchor" href="#&#x2171;&#x3001;mongoose"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2171;&#x3001;mongoose" class="plugin-anchor" href="#&#x2171;&#x3001;mongoose"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2161;&#x3001;Mongoose</h1>
<h3 id="1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"><a name="1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;" class="plugin-anchor" href="#1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mongoose&apos;</span>)
mongoose.connect(<span class="hljs-string">&apos;mongodb://localhost:27017/lagou-admin&apos;</span>, { useUnifiedTopology: <span class="hljs-literal">true</span>, useNewUrlParser: <span class="hljs-literal">true</span> })

<span class="hljs-keyword">const</span> Users = mongoose.model(<span class="hljs-string">&apos;users&apos;</span>, {
  username: <span class="hljs-built_in">String</span>,
  password: <span class="hljs-built_in">String</span>
})

<span class="hljs-built_in">module</span>.exports = {
  Users
}
</code></pre>
<h3 id="2&#x3001;route"><a name="2&#x3001;route" class="anchor-navigation-ex-anchor" href="#2&#x3001;route"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;route" class="plugin-anchor" href="#2&#x3001;route"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;Route</h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>)
<span class="hljs-keyword">var</span> router = express.Router()

<span class="hljs-keyword">const</span> { signup, hasUsername } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../controllers/users&apos;</span>)

router.post(<span class="hljs-string">&apos;/signup&apos;</span>, hasUsername, signup)

<span class="hljs-built_in">module</span>.exports = router
</code></pre>
<h3 id="3&#x3001;model"><a name="3&#x3001;model" class="anchor-navigation-ex-anchor" href="#3&#x3001;model"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;model" class="plugin-anchor" href="#3&#x3001;model"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;Model</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { Users } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../utils/db&apos;</span>)

<span class="hljs-keyword">const</span> save = (data) =&gt; {
  <span class="hljs-keyword">const</span> users = <span class="hljs-keyword">new</span> Users(data)
  <span class="hljs-keyword">return</span> users.save()
}

<span class="hljs-keyword">const</span> findOne = (conditions) =&gt; {
  <span class="hljs-keyword">return</span> Users.findOne(conditions)
}

<span class="hljs-built_in">module</span>.exports = {
  save,
  findOne
}
</code></pre>
<h3 id="4&#x3001;view"><a name="4&#x3001;view" class="anchor-navigation-ex-anchor" href="#4&#x3001;view"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x3001;view" class="plugin-anchor" href="#4&#x3001;view"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x3001;View</h3>
<p>art-template + express</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;ret&quot;</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-string">&quot;data&quot;</span>: {{data}}
}
</code></pre>
<h3 id="5&#x3001;controller"><a name="5&#x3001;controller" class="anchor-navigation-ex-anchor" href="#5&#x3001;controller"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x3001;controller" class="plugin-anchor" href="#5&#x3001;controller"><i class="fa fa-link" aria-hidden="true"></i></a>5&#x3001;Controller</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> usersModel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../models/users&apos;</span>)

<span class="hljs-keyword">const</span> signup = <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res, next</span>) </span>{
  res.set(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/json; charset=utf-8&apos;</span>)

  <span class="hljs-keyword">let</span> { username, password } = req.body

  <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">await</span> usersModel.save({
    username,
    password: hash
  })

  <span class="hljs-keyword">if</span> (result) {
    res.render(<span class="hljs-string">&apos;succ&apos;</span>, {
      data: <span class="hljs-built_in">JSON</span>.stringify({
        message: <span class="hljs-string">&apos;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x6210;&#x529F;.&apos;</span>
      })
    })
  } <span class="hljs-keyword">else</span> {
    res.render(<span class="hljs-string">&apos;fail&apos;</span>, {
      data: <span class="hljs-built_in">JSON</span>.stringify({
        message: <span class="hljs-string">&apos;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x5931;&#x8D25;.&apos;</span>
      })
    })
  }
}

<span class="hljs-keyword">const</span> hasUsername = <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req, res, next</span>) </span>{
  res.set(<span class="hljs-string">&apos;Content-Type&apos;</span>, <span class="hljs-string">&apos;application/json; charset=utf-8&apos;</span>)
  <span class="hljs-keyword">let</span> { username } = req.body
  <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">await</span> usersModel.findOne({username})
  <span class="hljs-keyword">if</span> (result) {
    res.render(<span class="hljs-string">&apos;fail&apos;</span>, {
      data: <span class="hljs-built_in">JSON</span>.stringify({
        message: <span class="hljs-string">&apos;&#x7528;&#x6237;&#x540D;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;.&apos;</span>
      })
    })
  } <span class="hljs-keyword">else</span> {
    next()
  }
}

<span class="hljs-built_in">module</span>.exports = {
  signup,
  hasUsername
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="04-Koa2.html" class="navigation navigation-prev " aria-label="Previous page: 04-Koa2">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="06-socket.html" class="navigation navigation-next " aria-label="Next page: 06-socket编程">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"05-MongoDB","level":"1.2.5","depth":2,"next":{"title":"06-socket编程","level":"1.2.6","depth":2,"path":"basics/06-socket.md","ref":"./basics/06-socket.md","articles":[]},"previous":{"title":"04-Koa2","level":"1.2.4","depth":2,"path":"basics/04-Koa2.md","ref":"./basics/04-Koa2.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","sharing-plus","anchor-navigation-ex","favicon","insert-logo","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/lurongtao/felixbooks-gp19-Node.js"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"insert-logo":{"url":"/felixbooks-gp19-node.js/images/node.js.jpg","style":"background: skyblue; width: 100%; height: 100%;"}},"theme":"default","author":"古艺散人","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"千锋大前端小册-GP19-Node.js","language":"zh-hans","links":{},"gitbook":"3.2.3","description":"Node.js复习"},"file":{"path":"basics/05-MongoDB.md","mtime":"2020-06-25T22:44:20.111Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-15T09:28:56.165Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

